require('newrelic')
require('rootpath')()
require('./biz-error')
const express = require('express')
const fundebug = require("fundebug-nodejs")
const pcApp = require('./pc/app')
const app = express()
const PORT = 3000
const PC_PORT = 5000
fundebug.apikey="3e6a4fefc8184a1a68480de11afb8e4d2b0850dfc043196081ed4fd4fae815a7"
fundebug.releaseStage = process.env.NODE_ENV || "development"
require('./jobs')
app.use('/heartbeat', (req, res, next) => {
  res.send('ok')
})
app.use('/op', require('./op/app'))
app.use('/v1', require('./api/app'))
app.use(fundebug.ExpressErrorHandler)
app.use((err, req, res, next) => {
  if (err instanceof BizError || !(err instanceof Error)) {
    res.status(400).send({message: err.message || err})
  } else {
    console.log(err.stack)
    res.status(500).send({message: '服务端错误'})
  }
})
pcApp.listen(PC_PORT, () => {
  console.log(`pc server started at ${PC_PORT}`)
})

app.listen(PORT, () => {
  console.log(`server started at ${PORT}`)
})

app.destroy = () => {
  const db = require('./db')
  db.destroy()
}

module.exports = app
